package com.mju.controller.common;

import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.mju.controller.ex.*;
import com.mju.service.ex.*;
import com.mju.utils.JWTUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 控制层的基类，用于封装公共的方法，比如方法
public class BaseController {
    // 操作成功的状态码
    public static final int OK = 200;
    // 文件大小
    public static final int AVATAR_MAX_SIZE = 9999999;
    // 文件类型
    public static final List<String> AVATAR_TYPE = List.of("image/jpg", "image/jpeg", "image/png");
    // 添加拦截业务层异常的方法
    // 注：一旦发现异常，那么会执行以下方法，并返回给前端。
    @ExceptionHandler({ServiceException.class, FileUploadException.class})
    public JsonResult<Void> handleException(Throwable e) {
        JsonResult<Void> result = new JsonResult<>();
        if (e instanceof UsernameDuplicatedException) {
            result.setStatus(4000);
            result.setMessage("用户名已经被占用");
        } else if (e instanceof InsertException) {
            result.setStatus(5000);
            result.setMessage("注册时，产生未知异常。");
        }else if (e instanceof UsernameNotFoundException) {
            result.setStatus(4001);
            result.setMessage("用户数据不存在");
        } else if (e instanceof PasswordNotMatchException) {
            result.setStatus(4002);
            result.setMessage("用户名或密码错误");
        }else if(e instanceof UpdateException) {
            result.setStatus(5001);
            result.setMessage("更新数据时，产生未知异常。");
        }else if (e instanceof FileEmptyException) {
            result.setStatus(6000);
            result.setMessage("文件不允许为空");
        } else if (e instanceof FileSizeException) {
            result.setStatus(6001);
            result.setMessage("文件大小超出限制");
        } else if (e instanceof FileTypeException) {
            result.setStatus(6002);
            result.setMessage("不支持该后缀的文件");
        } else if (e instanceof FileStatusException) {
            result.setStatus(6003);
            result.setMessage("文件大小超出限制");
        } else if (e instanceof FileUploadIOException) {
            result.setStatus(6004);
            result.setMessage("文件读写异常");
        } else if(e instanceof AddressCountLimitException) {
            result.setStatus(4003);
            result.setMessage("用户的收获地址超出上限");
        } else if(e instanceof AddressNotFoundException) {
            result.setStatus(4004);
            result.setMessage("用户的收获地址数据不存在");
        }else if(e instanceof AccessDeniedException) {
            result.setStatus(4005);
            result.setMessage("非法访问收获地址");
        }else if(e instanceof DeleteException) {
            result.setStatus(5002);
            result.setMessage("删除数据时产生未知异常");
        }else if (e instanceof ProductNotFoundException) {
            result.setStatus(4006);
            result.setMessage("访问的商品数据不存在的异常");
        }else if(e instanceof CartNotFoundException) {
            result.setStatus(4007);
            result.setMessage("购物车表不存在该商品的数据");
        }
        return result;
    }

    /**
     * 获取session对象中的uid
     * @param session session对象
     * @return 返回uid
     */
    public final Integer getIdFromSession(HttpSession session) {
        return Integer.valueOf(session.getAttribute("id").toString());
    }

    /**
     * 获取session对象中的username
     * @param session session对象
     * @return 返回用户名
     */
    public final String getUsernameFromSession(HttpSession session) {
        return session.getAttribute("username").toString();
    }

    /**
     * 将uid封装为jwt
     * @param uid
     * @return
     */
    public String getToken(String uid) {
        Map<String, String> map = new HashMap<>();
        map.put("uid",uid);
        return JWTUtils.getJWT(map);
    }

    /**
     * 将uid、uname封装为jwt
     * @param uid
     * @param uname
     * @return
     */
    public String getToken(String uid,String uname) {
        Map<String, String> map = new HashMap<>();
        map.put("uid",uid);
        map.put("uname",uname);
        return JWTUtils.getJWT(map);
    }

    /**
     * 根据token获取uid
     * @param token
     * @return
     */
    public String parseTokenGetUid(String token) {
        DecodedJWT decodedJWT = JWTUtils.parseJWT(token);
        Claim claim = decodedJWT.getClaim("uid");
        String uid = claim.asString();
        return uid;
    }
    /**
     * 根据token获取uid和uname
     * @param token
     * @return
     */
    public Map<String,String> parseTokenGetUidAndUname(String token) {
        DecodedJWT decodedJWT = JWTUtils.parseJWT(token);
        Claim c1 = decodedJWT.getClaim("uid");
        Claim c2 = decodedJWT.getClaim("uname");
        String uid = c1.asString();
        String uname = c2.asString();
        Map<String,String> map = new HashMap<>();
        map.put("uid", uid);
        map.put("uname", uname);
        return map;
    }
}
